package com.dingpeikeji.quickdeer.modules.wx.myinfo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.isouland.cpwx.api.core.util.SingleAccessToken;
import org.isouland.cpwx.api.user.WxUserAPI;
import org.isouland.cpwx.api.user.req.WxUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.count.entity.CountInfo;
import com.dingpeikeji.quickdeer.core.modules.count.service.TCountService;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Area;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Role;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.SysAds;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SysAdsService;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SystemService;
import com.dingpeikeji.quickdeer.mobile.service.TravelProductService;
import com.dingpeikeji.quickdeer.mobile.web.MobileJsonController;
import com.dingpeikeji.quickdeer.modules.base.service.FrontUserService;
import com.dingpeikeji.quickdeer.modules.interceptor.FrontBaseInterceptor;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotype;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroup;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FDemandsService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.FRentproductService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentgroupService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentproductService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;
import com.dingpeikeji.quickdeer.modules.wx.common.web.OAuth;
import com.dingpeikeji.quickdeer.utils.BackResult;
import com.dingpeikeji.quickdeer.utils.JedisClient;
import com.dingpeikeji.quickdeer.utils.JsonUtils;
import com.dingpeikeji.quickdeer.utils.StringUtil;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("${wxPath}/my")
public class MyInfoController extends BaseController{
	@Autowired
	private FrontUserService userService;
	@Autowired
	private SystemService systemService;
	
	@RequestMapping("myInfoPage")
	public String myInfoPage(Model model) {
		model.addAttribute("navStyle", "myInfoPage");
		return "wx/modules/base/myInfo";
	}
	@RequestMapping("modifyPwd")
	@ResponseBody
	public BackResult modifyPwd(String oldPassword,String newPassword,String repeatPassword,String userid){
		String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
		if (StringUtils.isNotBlank(oldPassword) && StringUtils.isNotBlank(newPassword) && StringUtils.isNotBlank(repeatPassword)){
			if(!newPassword.equals(repeatPassword)){
				return BackResult.build("400","两次密码输入不一致");
			}else if(!newPassword.matches(regex)){
				return BackResult.build("400","修改密码失败，密码格式为6-16位数字和字母的组合");
			} else  {
				User user = userService.get(userid);
				if (SystemService.validatePassword(oldPassword, user.getPassword())){
					systemService.updatePasswordById(user.getId(), user.getLoginName(), newPassword);
					return BackResult.build("200","修改密码成功");
				}else{
					return BackResult.build("400","修改密码失败，旧密码错误");
				}
			}
		} else {
			return BackResult.build("400","信息填写不完整");
		}
	}
	@RequestMapping("fpPage")
	public String fpPage(Model model) {
		return "wx/modules/rcpt/rcptList";
	}
	@RequestMapping("fxPage")
	public String fxPage(Model model) {
		return "wx/modules/orders/orderList_fx";
	}
	@RequestMapping("fxMesPage")
	public String fxMesPage(Model model) {
		return "wx/modules/orders/orderList_fx_mes";
	}
	@RequestMapping("settingPage")
	public String settingPage(Model model) {
		return "wx/modules/base/setting";
	}
	@RequestMapping("modifyPwdPage")
	public String modifyPwdPage(Model model) {
		return "wx/modules/base/modifyPwd";
	}
	@RequestMapping("fxBankInfo")
	public String fxBankInfo(Model model) {
		return "wx/modules/orders/orderList_fx_bank_info";
	}
}
